﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Text;
/// <summary>
/// Summary description for FormElementCollection
/// </summary>
public class FormElementCollection : Dictionary<string, string>
{
     public FormElementCollection(HtmlDocument htmlDoc)
        {
            var inputs = htmlDoc.DocumentNode.Descendants("input");
            foreach (var element in inputs)
            {
                AddInputElement(element);
            }

            var menus = htmlDoc.DocumentNode.Descendants("select");
            foreach (var element in menus)
            {
                AddMenuElement(element);
            }

            var textareas = htmlDoc.DocumentNode.Descendants("textarea");
            foreach (var element in textareas)
            {
                AddTextareaElement(element);
            }
        }
        private void AddInputElement(HtmlNode element)
        {
            string name = element.GetAttributeValue("name", "");
            string value = element.GetAttributeValue("value", "");
            string type = element.GetAttributeValue("type", "");

            if (string.IsNullOrEmpty(name)) return;

            switch (type.ToLower())
            {
                case "checkbox":
                case "radio":
                    if (!ContainsKey(name)) Add(name, "");
                    string isChecked = element.GetAttributeValue("checked", "unchecked");
                    if (!isChecked.Equals("unchecked")) this[name] = value;
                    break;
                default:
                    try
                    {
                        Add(name, value);
                    }
                    catch (Exception)
                    {
                    }
                    break;
            }
        }
        private void AddMenuElement(HtmlNode element)
        {
            string name = element.GetAttributeValue("name", "");
            var options = element.Descendants("option");

            if (string.IsNullOrEmpty(name)) return;
            try
            {
                // choose the first option as default
                var firstOp = options.First();
                string defaultValue = firstOp.GetAttributeValue("value", firstOp.NextSibling.InnerText);

                Add(name, defaultValue);
            }
            catch (Exception)
            {
            }
            // check if any option is selected
            foreach (var option in options)
            {
                string selected = option.GetAttributeValue("selected", "notSelected");
                if (!selected.Equals("notSelected"))
                {
                    string selectedValue = option.GetAttributeValue("value", option.NextSibling.InnerText);
                    this[name] = selectedValue;
                }
            }
        }
        private void AddTextareaElement(HtmlNode element)
        {
            string name = element.GetAttributeValue("name", "");
            if (string.IsNullOrEmpty(name)) return;
            Add(name, element.InnerText);
        }
        public string AssemblePostPayload()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var element in this)
            {
                string value = System.Web.HttpUtility.UrlEncode(element.Value);
                sb.Append("&" + element.Key + "=" + value);
            }
            return sb.ToString().Substring(1);
        }
    public FormElementCollection() 
    {
        //
        // TODO: Add constructor logic here
        //
    }
}
